﻿
using UnityEngine;

public class UIScaleButton : UIWidgetContainer
{
    public enum State
    {
        Normal,
        //Hover,
        Release,
        Pressed,
        Disabled,
    }

    public GameObject tweenTarget;
    public float duration = 0.2f;
    [System.NonSerialized]
    protected bool mInitDone = false;
    [System.NonSerialized]
    protected UIWidget mWidget;
    [System.NonSerialized]
    protected State mState = State.Normal;


    public State state { get { return mState; } set { SetState(value, false); } }
    public virtual bool isEnabled { get { return enabled; } set { enabled = value; } }

    void Start() { if (!mInitDone) OnInit(); if (!isEnabled) SetState(State.Disabled, true); }

    protected virtual void OnInit()
    {
        mInitDone = true;
        if (tweenTarget == null) tweenTarget = gameObject;
        if (tweenTarget != null) mWidget = tweenTarget.GetComponent<UIWidget>();
    }

    protected virtual void OnEnable()
    {
#if UNITY_EDITOR
        if (!Application.isPlaying)
        {
            mInitDone = false;
            return;
        }
#endif
        //if (mInitDone) OnHover(UICamera.IsHighlighted(gameObject));

        if (UICamera.currentTouch != null)
        {
            if (UICamera.currentTouch.pressed == gameObject) OnPress(true);
            //else if (UICamera.currentTouch.current == gameObject) OnHover(true);
        }
    }
    protected virtual void OnDisable()
    {
#if UNITY_EDITOR
        if (!Application.isPlaying) return;
#endif
        if (mInitDone && tweenTarget != null)
        {
            SetState(State.Normal, true);

            TweenScale ts = tweenTarget.GetComponent<TweenScale>();

            if (ts != null)
            {
                ts.value = Vector3.one;
                ts.enabled = false;
            }
        }
    }


    //protected virtual void OnHover(bool isOver)
    //{
    //    if (isEnabled)
    //    {
    //        if (!mInitDone) OnInit();
    //        if (tweenTarget != null) SetState(isOver ? State.Hover : State.Normal, false);
    //    }
    //}


    protected virtual void OnPress(bool isPressed)
    {
        if (isEnabled && UICamera.currentTouch != null)
        {
            if (!mInitDone) OnInit();

            if (tweenTarget != null)
            {
                if (isPressed)
                {
                    SetState(State.Pressed, false);
                }
                //else if (UICamera.currentTouch.current == gameObject)
                //{
                //if (UICamera.currentScheme == UICamera.ControlScheme.Controller)
                //{
                //    SetState(State.Hover, false);
                //}
                //else if (UICamera.currentScheme == UICamera.ControlScheme.Mouse && UICamera.hoveredObject == gameObject)
                //{
                //    SetState(State.Hover, false);
                //}
                //else SetState(State.Normal, false);
                //}
                else
                {
                    if (mState == State.Pressed) SetState(State.Release, false);
                    else SetState(State.Normal, false);
                }
            }
        }
        else if (!isEnabled && UICamera.currentTouch != null)
        {
            if (isPressed)
            {
                TweenScale ts = TweenScale.Begin(tweenTarget, 0.05f, Vector3.one * 0.9f);
                ts.SetOnFinished(delegate
                {
                    TweenScale.Begin(tweenTarget, 0.05f, Vector3.one * 1f);
                    ts.onFinished = new System.Collections.Generic.List<EventDelegate>();
                });
            }
        }
    }



    protected virtual void OnDragOver()
    {
        if (isEnabled)
        {
            if (!mInitDone) OnInit();
        }
    }


    protected virtual void OnDragOut()
    {
        if (isEnabled)
        {
            if (!mInitDone) OnInit();
            if (tweenTarget != null) SetState(State.Normal, false);
        }
    }

    public virtual void SetState(State state, bool instant)
    {
        if (!mInitDone)
        {
            mInitDone = true;
            OnInit();
        }

        if (mState != state)
        {
            mState = state;
            UpdateScale(instant);
        }
    }
    public void UpdateScale(bool instant)
    {
        TweenScale ts;

        if (tweenTarget != null)
        {
            switch (mState)
            {
                //case State.Hover: ts = TweenScale.Begin(tweenTarget, duration, Vector3.one); break;
                case State.Release:
                    ts = TweenScale.Begin(tweenTarget, duration, Vector3.one * 1.05f);
                    EventDelegate del = null;
                    del = new EventDelegate(delegate
                    {
                        tweenTarget.transform.localScale = Vector3.one;
                        ts.RemoveOnFinished(del);
                    });
                    ts.SetOnFinished(del);
                    break;
                case State.Pressed:
                    ts = TweenScale.Begin(tweenTarget, duration, Vector3.one * 0.9f);
                    break;
                case State.Disabled: ts = TweenScale.Begin(tweenTarget, duration, Vector3.one); break;
                default: ts = TweenScale.Begin(tweenTarget, duration, Vector3.one); break;
            }

            if (instant && ts != null)
            {
                ts.value = ts.to;
                ts.enabled = false;
            }
        }
    }
}
